package com.fancy.learncenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.EducationExperienceListAdapter;
import com.fancy.learncenter.adapter.WorkExperienceListAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.VitaDetailDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResumeActivity extends BaseActivity {


    WorkExperienceListAdapter workExperienceListAdapter;

    EducationExperienceListAdapter educationExperienceListAdapter;


    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.vita_name)
    TextView vitaName;
    @Bind(R.id.vita_age)
    TextView vitaAge;
    @Bind(R.id.iamge_user)
    SimpleDraweeView iamgeUser;
    @Bind(R.id.vita_account)
    TextView vitaAccount;
    @Bind(R.id.vita_education)
    TextView vitaEducation;
    @Bind(R.id.vita_experience)
    TextView vitaExperience;
    @Bind(R.id.vita_location)
    TextView vitaLocation;
    @Bind(R.id.edit_person)
    TextView editPerson;
    @Bind(R.id.work_recycleView)
    RecyclerView workRecycleView;
    @Bind(R.id.education_recycleView)
    RecyclerView educationRecycleView;
    @Bind(R.id.my_hight)
    EditText myHight;
    @Bind(R.id.work_name)
    TextView workName;
    @Bind(R.id.location_name)
    TextView locationName;
    @Bind(R.id.salary_name)
    TextView salaryName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resume, getResources().getColor(R.color.toolbar_color));

        ButterKnife.bind(this);
        initToolbar("我的简历");

        iamgeUser.setImageURI(SPUtils.getUserIcon());

        initWorkList();

        initEducaitonList();

    }


    private void initEducaitonList() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        educationRecycleView.setLayoutManager(linearLayoutManager);

        educationExperienceListAdapter = new EducationExperienceListAdapter(this, new ArrayList<VitaDetailDataBean.EduListBean>(), false);
        educationRecycleView.setAdapter(educationExperienceListAdapter);
    }


    private void initWorkList() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        workRecycleView.setLayoutManager(linearLayoutManager);

        workExperienceListAdapter = new WorkExperienceListAdapter(this, new ArrayList<VitaDetailDataBean.WorkListBean>(), false);
        workRecycleView.setAdapter(workExperienceListAdapter);
    }


    @Override
    protected void onResume() {
        super.onResume();

        getData();


    }

    VitaDetailDataBean vitaDetailDataBean;

    public void getData() {
        Map map = new HashMap();
        map.put("token", SPUtils.getToken());
        map.put("userId", SPUtils.getUserID());

        HttpMehtod.getInstance().getVita(map, new IdeaObserver<BaseDataBean<VitaDetailDataBean>>() {

            @Override
            public void onSuccess(BaseDataBean<VitaDetailDataBean> vitaDetailDataBeanBaseDataBean) {

                if (vitaDetailDataBeanBaseDataBean.getData() == null) {
                    return;
                }

                vitaDetailDataBean = vitaDetailDataBeanBaseDataBean.getData();
                if (TextUtils.isEmpty(vitaDetailDataBeanBaseDataBean.getData().getId())) {
                    resumeId = "";
                } else {
                    resumeId = vitaDetailDataBeanBaseDataBean.getData().getId();
                }


                vitaName.setText(vitaDetailDataBeanBaseDataBean.getData().getReal_name());

                vitaAge.setText(vitaDetailDataBeanBaseDataBean.getData().getAge() + "岁");

                vitaAccount.setText(vitaDetailDataBeanBaseDataBean.getData().getMobile());

                if (vitaDetailDataBeanBaseDataBean.getData().getLocation() != null) {
                    vitaLocation.setText(vitaDetailDataBeanBaseDataBean.getData().getLocation().getAreaname());
                    locationName.setText(vitaDetailDataBeanBaseDataBean.getData().getLocation().getAreaname());
                }

                workName.setText(vitaDetailDataBeanBaseDataBean.getData().getPosition());

                salaryName.setText(vitaDetailDataBeanBaseDataBean.getData().getSalary());

                vitaEducation.setText(vitaDetailDataBeanBaseDataBean.getData().getEducation());

                vitaExperience.setText(vitaDetailDataBeanBaseDataBean.getData().getWork_year());

                vitaName.setText(vitaDetailDataBeanBaseDataBean.getData().getReal_name());

                myHight.setText(vitaDetailDataBeanBaseDataBean.getData().getAdvantage());

                workExperienceListAdapter.notifyDataSetChanged((ArrayList<VitaDetailDataBean.WorkListBean>) vitaDetailDataBeanBaseDataBean.getData().getWork_list());

                educationExperienceListAdapter.notifyDataSetChanged((ArrayList<VitaDetailDataBean.EduListBean>) vitaDetailDataBeanBaseDataBean.getData().getEdu_list());

            }

            @Override
            public void onFail(int errorCode) {

            }

        });
    }


    String resumeId = "0";

    @OnClick({R.id.edit_person, R.id.confirm, R.id.apply_work, R.id.education_experience, R.id.education_more, R.id.woek_experience_more, R.id.work_experience})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.confirm:
                editPersonVita();
                break;
            case R.id.edit_person:
                intent = new Intent(this, PersonVitaActivity.class);
                intent.putExtra("resumeId", "" + resumeId);
                startActivity(intent);
                break;
            case R.id.apply_work:
                intent = new Intent(this, WorkLocationActivity.class);
                intent.putExtra("resumeId", "" + resumeId);
                startActivity(intent);
                break;
            case R.id.woek_experience_more:
                intent = new Intent(this, WorkExperienceListActivity.class);
                intent.putExtra("resumeId", "" + resumeId);
                startActivity(intent);
                break;
            case R.id.education_more:
                intent = new Intent(this, EducationExperienceListActivity.class);
                intent.putExtra("resumeId", "" + resumeId);
                startActivity(intent);
                break;
            case R.id.education_experience:
                intent = new Intent(this, EducationExperienceActivity.class);
                intent.putExtra("resumeId", "" + resumeId);
                startActivity(intent);

                break;
            case R.id.work_experience:
                intent = new Intent(this, WorkExperienceActivity.class);
                intent.putExtra("resumeId", "" + resumeId);
                startActivity(intent);
                break;
        }
    }

    private void getDatas() {

        HashMap map = new HashMap();
        map.put("resumeId", resumeId);
        HttpMehtod.getInstance().resumeInfo(map, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                ToastUtil.show("资料已更新");
                finish();
            }

            @Override
            public void onFail(int errorCode) {

            }
        });


    }


    private void editPersonVita() {
        Map map = new HashMap();
        map.put("resumeId", resumeId);
        if (TextUtils.isEmpty(myHight.getText().toString())) {
            ToastUtil.show("请填写我的优势");
            return;
        }
        map.put("advantage", myHight.getText().toString());

        HttpMehtod.getInstance().editPersonVita(map, new IdeaObserver<BaseDataBean>() {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                ToastUtil.show("资料已更新");
                finish();
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }

}
